/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package cn.com.recloud.bbs.service.impl;

import cn.com.recloud.bbs.constant.MetaOptionConstant;
import cn.com.recloud.bbs.entity.bbs_opened_log;
import cn.com.recloud.bbs.dao.OpenedLogDao;
import cn.com.recloud.bbs.entity.metaoption;
import cn.com.recloud.bbs.service.MetaoptionService;
import cn.com.recloud.bbs.service.OpenedLogService;
import cn.com.recloud.bbs.util.UserUtil;
import cn.com.recloud.bbs.vo.OpenLogVO;
import cn.com.recloud.core.constant.SysConstant;
import cn.com.recloud.one.spring.service.impl.BaseServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author herry
 * @date 2020-06-08
 */
@Service
public class OpenedLogServiceImpl extends BaseServiceImpl<OpenedLogDao, bbs_opened_log> implements OpenedLogService {

    @Resource
    private MetaoptionService metaoptionService;

    @Resource
    private OpenedLogDao openedLogDao;

    @Autowired
    private UserUtil userUtil;
    /**
     * 记录文章活动被打开记录
     */
    @Override
    public boolean saveOpenLog(bbs_opened_log bbsOpenedLog,String type) {
        if("campaign".equals(type)){
            bbsOpenedLog.setObject_type_name(MetaOptionConstant.CAMPAIGN_ENTITY_NAME);
        }else{
            bbsOpenedLog.setObject_type_name(MetaOptionConstant.ARTICAL_ENTITY_NAME);
        }
        String userName = userUtil.getUserName();
        String userId = userUtil.getUserId();
        metaoption metaoption = null;
        //未获取到用户信息则用户未登录
        //未登录就是联系人，登录则是系统用户
        if(SysConstant.DEFAULT_ID.equals(userId)){
            metaoption = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.SIGNUP_TYPE,"1");
        }else{ //用户登录
            metaoption = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.SIGNUP_TYPE,"2");
        }
        bbsOpenedLog.setReader_type(metaoption.getValue());
        bbsOpenedLog.setReader_typename(metaoption.getName());
        bbsOpenedLog.setReader_id(userId);
        bbsOpenedLog.setReader_name(userName);
        bbsOpenedLog.setName(bbsOpenedLog.getObject_name());
        return super.save(bbsOpenedLog);
    }

    @Override
    public List<OpenLogVO> findOpendList(String name,String type,int pageIndex,int pageSize) {
        String objectTypeName = null;
        Page<OpenLogVO> page = new Page<>(pageIndex, pageSize);
        String userId = userUtil.getUserId();
        //1为活动2为文章
        if("1".equals(type)){
            objectTypeName = MetaOptionConstant.CAMPAIGN_ENTITY_NAME;
            return openedLogDao.findOpendCampaignList(objectTypeName,name,userId,page);
        }else{
            objectTypeName = MetaOptionConstant.ARTICAL_ENTITY_NAME;
            return openedLogDao.findOpendArticleList(objectTypeName,name,userId,page);
        }
    }
}
